$(function() {     
	$.post(getUrlBySuffix("/menuAdmin/getAllRole"), {}, function(data) {
		console.log(data);
		var list = eval(data);
		var _html = ""
		for (var i = 0; i < list.length; i++) {
			var _temp = "<option value='" + list[i].id + "' roleName='"
					+ list[i].name + "' data-tokens='" + list[i].descriptor
					+ "'>" + list[i].descriptor + "</option>"
			_html += _temp;
		}

		$("#roleList").attr("data-live-search", "true");
		$("#roleList").append(_html);
		$("#roleList").selectpicker('refresh');
	})

	$("#roleList").on("change", function() {
		var thisRodeId = $(this).find("option:selected").val();
		$("input[name=roleId]").val(thisRodeId);
	});
	
	//一级菜单总数量
	var menuRoleSize = $("#menuRoleSize").val();
	roleAllSelectCheckBox();
	roleAllGiveUpCheckBox();
	menuRoleAllSelectCheckBox(menuRoleSize);
	
	
	

	$("#commitBtn").on("click", function() {
		if($("input[class=menuRoleCheckBox]:checked").length==0){
			swal("提示", "请选择一个菜单");
			return false;
		}
		if($("input[name=roleId]").val()==""){
			swal("提示", "请选择一个角色");
			return false;
		}
		var params = {
			menuIds : requestData().join(","),
			roleId : Number($("input[name=roleId]").val())
		}
		$.post(getUrlBySuffix("/menuAdmin/insertMenuRoleList"),params,function(json){
			if (json.success) {
				 swal({
		                title:"提示",
		                text:json['message'],
		                type:"success",
		                confirmButtonText:"确定"
		        },function(){
		        	location.reload();
		        });
			}else {
				swal({
					title : "提示",
					text : json['message'],
					type : "error"
				});
			}
		})
	})

})
/**
 * 全选 checkBox
 * */
function roleAllSelectCheckBox(){
	$("#roleAllSelectCheckBox").click(function(){
		$.each($(".menuRoleCheckBox"),function(){
			this.checked = true;
		})
	});
}

/**
 * 全不选
 * */
function roleAllGiveUpCheckBox(){
	$("#roleAllGiveUpCheckBox").click(function(){
		$.each($(".menuRoleCheckBox"),function(){
			this.checked = false;
		})
	});
}

function menuRoleAllSelectCheckBox(menuRoleSize){
	for(var i=1;i<=menuRoleSize;i++){
		$("#selectCheckBox_"+i).click(function(){
			$.each($(this).parent().find(".menuRoleCheckBox"),function(){
				this.checked = true;
			});
		});
		$("#giveUpCheckBox_"+i).click(function(){
			$.each($(this).parent().find(".menuRoleCheckBox"),function(){
				this.checked = false;
			});
		});
	}
}
/**
 * 获取被选中的复选框的value值 
 * @param checkArray
 * */
function requestData(){
	var checkArray = new Array();
	$('input[class="menuRoleCheckBox"]:checked').each(function(){
		checkArray.push($(this).val());
	})
	return checkArray;
}
function getMenuId(menuId) {
	$("input[name=menuId]").val(menuId);
}

function getMenuList(level, pid, elementId) {
	var params = {
		level : level,
		pid : pid
	}
	$.post(getUrlBySuffix("/menuAdmin/getMenuInfo"), params,
			function(data) {
				var list = eval(data);
				var _html = "";
				var thishtml = $("#" + elementId + "").children().first().prop(
						"outerHTML");
				;
				console.log(thishtml);
				for (var i = 0; i < list.length; i++) {

					var _tempHtml = "<option menuId='" + list[i].id
							+ "' menuLevel='" + list[i].menuLevel + "'>"
							+ list[i].menuName + "</option>";
					_html += _tempHtml;
				}
				$("#" + elementId).html("");
				$("#" + elementId).html(thishtml + _html);
				$("#" + elementId).selectpicker('refresh');
			});

}
